﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using PageEntity;
using UnitWorkContext;
using OA.DataBase;
using Utility;
using EntityModel;

namespace BLLImp
{
    public class ModuleServer : IModuleServer
    {

        public List<ModuleDTO> LoadLeftMenu()
        {
            List<ModuleDTO> result = new List<ModuleDTO>();
            using (IUnitOfWork unitwork = new UnitOfWork(new OAContext()))
            {
                var modules = unitwork.ModuleRepository.Get(m => m.Parent == null).ToList();
                if (modules != null && modules.Count > 0)
                {
                    foreach (var Module in modules)
                    {
                        object node = new ModuleDTO();
                        BeanUtils.CopyObject(ref node, (Module)Module);
                        result.Add((ModuleDTO)node);
                        var Children = unitwork.ModuleRepository.Get(m => m.ParentId == Module.ModuleID).ToList();
                        if (Children.Count > 0)
                        {
                            foreach (var _CModule in Children)
                            {
                                object cnode = new ModuleDTO();
                                BeanUtils.CopyObject(ref cnode, _CModule);
                                ((ModuleDTO)node).Children.Add((ModuleDTO)cnode);
                            }
                        }
                    }
                }
            }
            return result;
        }
        public DataGridDTO LoadMoudleGridTree(long? parentid)
        {
            DataGridDTO result = new DataGridDTO();
            IList<ModuleDTO> treenodes = new List<ModuleDTO>();
            using (IUnitOfWork unitwork = new UnitOfWork(new OAContext()))
            {
                var modules = unitwork.ModuleRepository.All().Where(p => p.ParentId == parentid);
                if (modules != null)
                {
                    foreach (var _module in modules)
                    {
                        object node = new ModuleDTO();
                        BeanUtils.CopyObject(ref node, (Module)_module);
                        if (_module.ParentId.HasValue)
                            ((ModuleDTO)node)._parentId = _module.ParentId.Value;
                        ((ModuleDTO)node).state = unitwork.ModuleRepository.All().Where(p => p.ParentId == _module.ModuleID).Count() > 0 ? "closed" : "open";
                        treenodes.Add((ModuleDTO)node);
                    }
                }
            }
            result.rows = treenodes;
            return result;
        }
        public List<TreeNodeDTO> LoadModuleTree(long? parentid)
        {
            IEnumerable<Module> modules;
            List<TreeNodeDTO> treenodes = new List<TreeNodeDTO>();
            using (IUnitOfWork unitwork = new UnitOfWork(new OAContext()))
            {
                if (parentid == 0)
                {
                    modules = unitwork.ModuleRepository.All().Where(p => p.ParentId == null);
                }
                else
                {
                    modules = unitwork.ModuleRepository.All().Where(p => p.ParentId == parentid);
                }

                if (modules != null && modules.Count() > 0)
                {
                    foreach (var _module in modules)
                    {
                        TreeNodeDTO node = new TreeNodeDTO()
                        {
                            id = _module.ModuleID,
                            text = _module.ModuleName,
                            state = unitwork.ModuleRepository.All().Where(p => p.ParentId == _module.ModuleID).Count() > 0 ? "closed" : "open"
                        };

                        treenodes.Add(node);
                    }
                }
            }
            return treenodes;
        }
        public ModuleDTO Save(ModuleDTO mode)
        {
            Module _mode = new Module()
            {
                ModuleName = mode.ModuleName,
                iconCls = mode.iconCls,
                Href = mode.Href,
                CheckInTime = DateTime.Now,
                DataState = true,
                Sort = mode.Sort

            };
            if (mode._parentId > 0)
            {
                _mode.ParentId = mode._parentId;
            }
            using (IUnitOfWork unitwork = new UnitOfWork(new OAContext()))
            {
                unitwork.ModuleRepository.Insert(_mode);
                unitwork.Save();
                mode.ModuleID = _mode.ModuleID;
                return mode;
            }
        }
        public bool Delet(ModuleDTO mode)
        {
            using (IUnitOfWork unitwork = new UnitOfWork(new OAContext()))
            {
                Module _mode = new Module()
                {
                    ModuleID = mode.ModuleID
                };
                unitwork.ModuleRepository.Delet(_mode);
                unitwork.Save();
                return true;

            }
        }
    }
}
